﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading;
using System.Drawing;
using System.Windows.Media;

using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace Jumper
{
    class Jumper : HBPlugin
    {
        public bool _jump = true;           // Do Jump
        public bool _blink = true;          // Do use Blink
        public bool _forceblink = false;    // Force use of Blink for lvl < 25
        public float _jumpdistance = 8;     // Distance to next CTM target to use Jump
        public float _blinkdistance = 25;   // Distance to next CTM target to use Blink
        public int _randchance = 5;         // 5% Chance to "Jump" after check
        public int _sleepcycle = 1250;      // Sleepcycle to minimize Spam, shouldn't be lower than GC

        public bool debug = false;          // Activate Debugoutput
        
        public override string Name { get { return "Jumper"; } }
        public override string Author { get { return "glookie1"; } }
        public override Version Version { get { return new Version(0, 3, 1903, 0); } }

        public override bool WantButton { get { return true; } }
        public override string ButtonText { get { return "N/A"; } }

        private static LocalPlayer Me = StyxWoW.Me;
        public WoWClass _class;
        public int _level;
        private Random rand;
        private int _rand;
        private DateTime sleep;
        private bool _blinklag;

        //Logging Class for your conviance
        public static void slog(string format, params object[] args)
        {
            Logging.Write("[Jumper] " + format, args);
        }

        private string _true(bool test)
        {
            if (test == true)
            {
                return "activated";
            }
            else
            {
                return "deactivated";
            }
        }

        public override void Initialize()
        {
            base.Initialize();
            _class = Me.Class;
            _level = Me.Level;
            rand = new Random();
            slog("You are a level " + _level + " " + _class + ".");
            // Check for Blink

            #region Blink

            if (Me.Class != WoWClass.Mage )
            {
                _blink = false;
            }

            _blinklag = false;
            slog("Blinking is " + _true(_blink) + "!");
            
            #endregion

            // Jumping

            #region Jump

            slog("Jumping is " + _true(_jump) + "!");

            #endregion

            sleep = DateTime.Now;
            return;
        }

        public override void OnButtonPress()
        {
            return;
        }
        
        public override void Dispose()
        {
            slog("Deactivate Jumper...");
            base.Dispose();
        }

        public override void Pulse()
        {
            if (!Me.IsDead && !Me.Mounted && !Me.IsResting && !Me.IsActuallyInCombat && !Me.IsCasting && Me.IsMoving && !Me.IsIndoors 
                && !Me.IsFlying && !Me.IsOnTransport && !Me.OnTaxi && !Battlegrounds.IsInsideBattleground && !Me.IsInInstance && !Me.IsSwimming)
            {
                // Blinking

                #region Blinking

                if (_blink && Me.Location.Distance(WoWMovement.ClickToMoveInfo.ClickPos) > _blinkdistance 
                    && SpellManager.CanCast(1953) && !SpellManager.GlobalCooldown)
                {
                    if (debug)
                    {
                        slog("Blink: " );
                    }
                    if (_blinklag == true)
                    {
                        SpellManager.CastSpellById(1953);
                        _blinklag = false;
                        Thread.Sleep(_sleepcycle);
                        return;
                    }
                    else if (_blinklag == false)
                    {
                        _blinklag = true;
                        Thread.Sleep(75); // Hardcoded Sleep
                        return;
                    }
                }

                #endregion

                // Jumping

                #region Jumping

                if (_jump && Me.Location.Distance(WoWMovement.ClickToMoveInfo.ClickPos) > _jumpdistance)
                {
                    _rand = rand.Next(100);
                    if (debug)
                    {
                        slog("Random Jump: " + _rand + " | " + _true(_rand < _randchance));
                    }
                    if (_rand < _randchance)
                    {
                        WoWMovement.Move(WoWMovement.MovementDirection.JumpAscend);
                        Thread.Sleep(_sleepcycle);
                        return;
                    }
                }

                #endregion

                // Reset Lag
                _blinklag = false;
            }
            return;
        }

    }
}